﻿using GKQC.Common;
using GKQC.DbHelper.DbModels;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.Util;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace GKQC.Ort.Reports
{
    /// <summary>
    /// 信赖性试验：BDV测试
    /// </summary>
    public class R021 : BaseReport
    {
        public override void GenReportSheet(IWorkbook workbook, ISheet sheet, OrtHeadModel head)
        {
            #region 表头
            int headColumnsIndex = 0;

            for (int i = 0; i < 1; i++)
            {
                try
                {
                    //sheet.GetRow(1).GetCell(headColumnsIndex + 33).SetCellValue(head.LcMax);

                    //1.Customer:
                    if (sheet.GetRow(2).GetCell(headColumnsIndex + 0) != null)
                        sheet.GetRow(2).GetCell(headColumnsIndex + 0).SetCellValue(
                            "  Customer :" + head.CustomerCode);
                    sheet.GetRow(2).GetCell(headColumnsIndex + 6).SetCellValue(
                        "Date: " + head.TestDate.ToString("yyyy/M/d"));
                    
                    sheet.GetRow(3).GetCell(headColumnsIndex + 0).SetCellValue(
                        "  Lot No：" + head.LotNo);
                    sheet.GetRow(3).GetCell(headColumnsIndex + 7).SetCellValue(head.Pn);
                    sheet.GetRow(3).GetCell(headColumnsIndex + 12).SetCellValue(head.Size);

                    sheet.GetRow(4).GetCell(headColumnsIndex + 0).SetCellValue(
                        string.Format("  Sample Q'ty: {0} pcs" , head.ElectricQty));

                    sheet.GetRow(6).GetCell(headColumnsIndex + 8).SetCellValue(
                        string.Format("less than:  {0}  µA", head.LcMax));
                    sheet.GetRow(7).GetCell(headColumnsIndex + 8).SetCellValue(
                        string.Format(" {0} ~ {1}µF", head.Standard.CapMin, head.Standard.CapMax));
                    sheet.GetRow(8).GetCell(headColumnsIndex + 8).SetCellValue(
                        string.Format("less than:  {0}", head.DfMax / 100d));


                    if (head.IsValid)
                        sheet.GetRow(22).GetCell(headColumnsIndex + 2).SetCellValue("合格");
                    else
                        sheet.GetRow(22).GetCell(headColumnsIndex + 2).SetCellValue("不合格");

                    //Tested by:王燕
                    sheet.GetRow(23).GetCell(headColumnsIndex + 14).SetCellValue("Tested by:" + head.CreateUser);
                }
                catch (Exception ex)
                {
                    Logger.LogError(ex);
                }
            }
            #endregion 表头

            #region 细表
            int currentDataColumnIndex = 1;
            int dataRowIndex = 15;


            for (int i = 0; i < head.Dtls.Count; i++)
            {
                if (i > 0) break;

                try 
                {
                    #region data
                    int cIndex = currentDataColumnIndex;
                    var caps = head.Dtls[i].Datas.Where(d => d.ItemCode == "CAP");
                    if (caps.Any())
                    {
                        var carpss = caps.ToList();
                        ICellStyle lcCellStyle = workbook.CreateCellStyle();
                        lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                        IDataFormat lcdataformat = workbook.CreateDataFormat();
                        double maxlc = carpss.Max(t => t.Value);
                        lcCellStyle.DataFormat = lcdataformat.GetFormat("0.00");
                        //int capNum = OrtReportFactory.GetCapFormatNum(maxlc);
                        for (int k = 0; k < carpss.Count; k++)
                        {
                            if (k >= head.ElectricQty) break;
                            if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                                continue;
                            if (carpss[k].Value != 0)
                                sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(carpss[k].Value);
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                        }
                        sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                    }

                    cIndex++;
                    var dfs = head.Dtls[i].Datas.Where(d => d.ItemCode == "DF");
                    if (dfs.Any())
                    {
                        var dfss = dfs.ToList();
                        for (int k = 0; k < dfss.Count; k++)
                        {
                            if (k >= head.ElectricQty) break;
                            if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                                continue;
                            if (dfss[k].Value != 0)
                                sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(Math.Round(dfss[k].Value / 100, 4));
                        }
                    }
                    
                    cIndex++;
                    var lcs = head.Dtls[i].Datas.Where(d => d.ItemCode == "LC");
                    if (lcs.Any())
                    {
                        var lcss = lcs.ToList();
                        ICellStyle lcCellStyle = workbook.CreateCellStyle();
                        lcCellStyle.CloneStyleFrom(sheet.GetRow(dataRowIndex).GetCell(cIndex).CellStyle);
                        IDataFormat lcdataformat = workbook.CreateDataFormat();
                        double maxlc = lcss.Max(t => t.Value);
                        lcCellStyle.DataFormat = lcdataformat.GetFormat(OrtReportFactory.GetLcNumFormat(maxlc));
                        //int lcNum = OrtReportFactory.GetLcFormatNum(maxlc);
                        for (int k = 0; k < lcss.Count; k++)
                        {
                            if (k >= head.ElectricQty) break;
                            if (sheet.GetRow(dataRowIndex + k) == null || sheet.GetRow(dataRowIndex + k).GetCell(cIndex) == null)
                                continue;
                            if (lcss[k].Value != 0)
                                sheet.GetRow(dataRowIndex + k).GetCell(cIndex).SetCellValue(lcss[k].Value);
                            sheet.GetRow(dataRowIndex + k).GetCell(cIndex).CellStyle = lcCellStyle;
                        }
                        sheet.GetRow(dataRowIndex + head.ElectricQty).GetCell(cIndex).CellStyle = lcCellStyle;
                    }

                    #endregion data
                    cIndex = 0;
                    sheet.GetRow(dataRowIndex - 2).GetCell(cIndex + 5).SetCellValue(
                            Math.Round(head.Voltage, 1) + "V");
                    sheet.GetRow(dataRowIndex - 2).GetCell(cIndex + 6).SetCellValue(
                            Math.Round(head.Voltage * (1 + 1 * 0.2), 1) + "V");
                    sheet.GetRow(dataRowIndex - 2).GetCell(cIndex + 7).SetCellValue(
                            Math.Round(head.Voltage * (1 + 2 * 0.2), 1) + "V");
                    sheet.GetRow(dataRowIndex - 2).GetCell(cIndex + 8).SetCellValue(
                            Math.Round(head.Voltage * (1 + 3 * 0.2), 1) + "V");
                    sheet.GetRow(dataRowIndex - 2).GetCell(cIndex + 9).SetCellValue(
                            Math.Round(head.Voltage * (1 + 4 * 0.2), 1)+ "V");
                    sheet.GetRow(dataRowIndex - 2).GetCell(cIndex + 10).SetCellValue(
                            Math.Round(head.Voltage * (1 + 5 * 0.2), 1) + "V");
                    sheet.GetRow(dataRowIndex - 2).GetCell(cIndex + 11).SetCellValue(
                            Math.Round(head.Voltage * (1 + 6 * 0.2), 1) + "V");
                    sheet.GetRow(dataRowIndex - 2).GetCell(cIndex + 12).SetCellValue(
                            Math.Round(head.Voltage * (1 + 7 * 0.2), 1) + "V");
                    sheet.GetRow(dataRowIndex - 2).GetCell(cIndex + 13).SetCellValue(
                            Math.Round(head.Voltage * (1 + 8 * 0.2), 1) + "V");
                    sheet.GetRow(dataRowIndex - 2).GetCell(cIndex + 14).SetCellValue(
                            Math.Round(head.Voltage * (1 + 9 * 0.2), 1) + "V");


                    for (int k = 0; k < head.Volts.Count; k++)
                    {
                        if (k >= head.ElectricQty) break;
                        var v = head.Volts[k];
                        sheet.GetRow(dataRowIndex + k).GetCell(cIndex + 5).SetCellValue(v.V1);
                        sheet.GetRow(dataRowIndex + k).GetCell(cIndex + 6).SetCellValue(v.V2);
                        sheet.GetRow(dataRowIndex + k).GetCell(cIndex + 7).SetCellValue(v.V3);
                        sheet.GetRow(dataRowIndex + k).GetCell(cIndex + 8).SetCellValue(v.V4);
                        sheet.GetRow(dataRowIndex + k).GetCell(cIndex + 9).SetCellValue(v.V5);
                        sheet.GetRow(dataRowIndex + k).GetCell(cIndex + 10).SetCellValue(v.V6);
                        sheet.GetRow(dataRowIndex + k).GetCell(cIndex + 11).SetCellValue(v.V7);
                        sheet.GetRow(dataRowIndex + k).GetCell(cIndex + 12).SetCellValue(v.V8);
                        sheet.GetRow(dataRowIndex + k).GetCell(cIndex + 13).SetCellValue(v.V9);
                        sheet.GetRow(dataRowIndex + k).GetCell(cIndex + 14).SetCellValue(v.V10);
                        sheet.GetRow(dataRowIndex + k).GetCell(cIndex + 15).SetCellValue(v.Remark);
                    }
                }
                catch(Exception ex)
                {
                    Logger.LogError(ex);
                }
            }

            #endregion


            sheet.ForceFormulaRecalculation = true;
        }
    }
}
